# ========== 第一部分：层次聚类分析 ==========
# 设置工作目录
setwd("D:/Rassignment/第三次作业")  # 使用正斜杠避免转义问题

# 加载包
library(openxlsx)

# 读取数据
ADdata <- read.xlsx("merged_data.xlsx")

# 数据处理
rownames(ADdata) <- ADdata$X  # 设置行名
ADdata$X <- NULL  # 移除原始ID列
ADdata_matrix <- as.matrix(ADdata)  # 转换为矩阵

# 计算距离和聚类
ADdata_dist <- dist(ADdata_matrix, method = "euclidean")
ADdata_tree <- hclust(ADdata_dist, method = "average")

# 绘制聚类图
plot(ADdata_tree, 
     main = "ADdata层次聚类分析", 
     xlab = "", 
     sub = "",
     hang = -1,  # 所有标签对齐
     cex = 0.6,  # 标签大小
     labels = rownames(ADdata_matrix))  # 明确指定标签

# ========== 第二部分：火山图绘制 ==========
# 设置工作目录
setwd("D:/Rassignment/第四次作业")

# 加载数据
load("volcano.RData")  # 加载后包含prostat对象

# 加载包
library(ggplot2)

# 创建火山图数据
volcano_data <- data.frame(
  ID = prostat$ID,
  FC = prostat$FC,
  negativelog10p = -log10(prostat$P),
  diffexp = "No"
)

# 设置差异表达分类
up_threshold <- log2(1.2)
down_threshold <- log2(1/1.2)
p_threshold <- -log10(0.05)

volcano_data$diffexp[
  volcano_data$negativelog10p > p_threshold & 
    volcano_data$FC > up_threshold
] <- "Up"

volcano_data$diffexp[
  volcano_data$negativelog10p > p_threshold & 
    volcano_data$FC < down_threshold
] <- "Down"

# 设置因子水平确保正确排序
volcano_data$diffexp <- factor(volcano_data$diffexp, 
                               levels = c("Down", "No", "Up"))

# 创建火山图
volcano_plot <- ggplot(volcano_data, aes(x = FC, y = negativelog10p, color = diffexp)) +
  geom_point(alpha = 0.6, size = 1.5) +
  scale_color_manual(values = c(Down = "blue", No = "grey", Up = "red")) +
  geom_vline(xintercept = c(down_threshold, up_threshold), 
             linetype = "dashed", color = "black", linewidth = 0.3) +
  geom_hline(yintercept = p_threshold, 
             linetype = "dashed", color = "black", linewidth = 0.3) +
  labs(x = expression(log[2]~Fold~Change), 
       y = expression(-log[10]~P~value),
       color = "Expression") +
  theme_minimal()  # 添加简洁的主题

# 显示图形
print(volcano_plot)

# 保存图形
ggsave(
  filename = "volcano_plot.jpg",
  plot = volcano_plot,
  width = 8,
  height = 6,
  dpi = 300
)